package com.cl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.blog.common.Const;
import com.cl.blog.entity.BCommunity;
import com.cl.blog.entity.BCommunityReview;
import com.cl.blog.mapper.BCommunityMapper;
import com.cl.blog.service.BCommunityReviewService;
import com.cl.blog.mapper.BCommunityReviewMapper;
import com.cl.blog.service.BCommunityService;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.CommunityReviewVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author object
 * @description 针对表【b_community_review】的数据库操作Service实现
 * @createDate 2024-03-14 17:13:59
 */
@Service
public class BCommunityReviewServiceImpl extends ServiceImpl<BCommunityReviewMapper, BCommunityReview>
		implements BCommunityReviewService {

	@Autowired
	JwtUtil jwtUtil;
	@Autowired
	BCommunityMapper bCommunityMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean insertReview(CommunityReviewVo reviewVo, HttpServletRequest request) {
		System.out.println(reviewVo);
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);
		BCommunityReview bCommunityReview = new BCommunityReview();
		if (reviewVo.getPuserId() != null) {
			bCommunityReview.setParentId(reviewVo.getPuserId());
		}
		bCommunityReview.setCommunityReviewContent(reviewVo.getContent());
		bCommunityReview.setCreateTime(LocalDateTime.now());
		bCommunityReview.setUserId(Integer.valueOf(claim.getSubject()));
		bCommunityReview.setLikeCount(0L);
		bCommunityReview.setCommunityId(reviewVo.getCommunityId());
		bCommunityMapper.update(null, new UpdateWrapper<BCommunity>()
				.eq("id", reviewVo.getCommunityId())
				.setSql("review_count=review_count + 1"));

		System.out.println("回复的消息--->" + bCommunityReview);
		return this.save(bCommunityReview);
	}
}




